package com.android.lovely.ui.activity

import android.app.Activity
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.os.Bundle
import androidx.core.text.buildSpannedString
import com.android.lovely.R
import com.android.lovely.databinding.ActivityInviteBinding
import com.android.lovely.ext.copy
import com.android.lovely.ext.share
import com.android.lovely.ext.windowBackgroundNav
import com.android.lovely.framework.BaseActivity
import com.android.lovely.framework.UIController
import com.kotlin.extensions.context.ActivityExt.transparentStatusBar
import com.kotlin.extensions.view.ViewExt.addStatusBarMarginTop
import com.kotlin.extensions.view.ViewExt.clickable

class InviteActivity : BaseActivity<ActivityInviteBinding>() {

    private val code by lazy { intent.getStringExtra("code").orEmpty() }

    private val shareContent by lazy { intent.getStringExtra("share_content").orEmpty() }

    override fun initView(savedInstanceState: Bundle?) {
        transparentStatusBar()
        windowBackgroundNav()
        mBinding.back.addStatusBarMarginTop()
        mBinding.back.clickable { onBackPressedDispatcher.onBackPressed() }
        mBinding.code.text = code.toCharArray().joinToString("\t")
        mBinding.copy1.clickable {
            shareContent.copy(this)
        }
        mBinding.copyBtn.clickable {
            shareContent.copy(this)
        }
        mBinding.shareBtn.clickable {
            shareContent.share()
        }
        mBinding.eventTv.text = buildSpannedString {
            append(getString(R.string.event1))
            appendLine()
            appendLine()
            append(getString(R.string.event2))
            appendLine()
            appendLine()
            append(getString(R.string.event3))
            appendLine()
            appendLine()
            append(getString(R.string.event4))
        }
    }

    private fun String.copy(context: Activity) {
        val clipboardManager =
            context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
        val clipData = ClipData.newPlainText("label", this)
        clipboardManager.setPrimaryClip(clipData)
        UIController.showToast(context.getString(R.string.copied_code))
    }

    override var transparentSystemBar: Boolean = false
}